package com.kifas.service.dataaccess;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.kifas.entity.BukuVoucherEntity;

@Repository
public class BukuVoucherDao {

	@Autowired
	private SessionFactory sessionFactory;

	private static final String ALL_BUKU_VOUCHER = "from BukuVoucherEntity bv ";
	private static final String BUKU_VOUCHER_BY_ID = ALL_BUKU_VOUCHER
			+ " where bv.idBukuVoucher = :idBukuVoucher";

	public void insert(BukuVoucherEntity bukuVoucherEntity) {
		sessionFactory.getCurrentSession().save(bukuVoucherEntity);
	}

	public void update(BukuVoucherEntity bukuVoucherEntity) {
		sessionFactory.getCurrentSession().update(bukuVoucherEntity);
	}

	@SuppressWarnings("unchecked")
	public List<BukuVoucherEntity> getBukuVouchers() {
		return sessionFactory.getCurrentSession().createQuery(ALL_BUKU_VOUCHER)
				.list();
	}

	public BukuVoucherEntity getBukuVoucher(Long idBukuVoucher) {
		return (BukuVoucherEntity) sessionFactory.getCurrentSession()
				.createQuery(BUKU_VOUCHER_BY_ID)
				.setLong("idBukuVoucher", idBukuVoucher).uniqueResult();
	}
}
